package ca.uhn.fhir.rest.server;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.test.utilities.HttpClientExtension;
import ca.uhn.fhir.test.utilities.server.RestfulServerExtension;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Binary;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Reference;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class 	BinaryServerR4Test {
	private static final FhirContext ourCtx = FhirContext.forR4Cached();
	private static Binary ourLastBinary;
	private static byte[] ourLastBinaryBytes;
	private static String ourLastBinaryString;
	private static IdType ourLastId;
	private static Binary ourNextBinary;

	@RegisterExtension
	public static final RestfulServerExtension ourServer = new RestfulServerExtension(ourCtx)
		 .registerProvider(new BinaryProvider())
		 .withPagingProvider(new FifoMemoryPagingProvider(100))
		 .setDefaultResponseEncoding(EncodingEnum.XML)
		 .setDefaultPrettyPrint(false);

	@RegisterExtension
	public static final HttpClientExtension ourClient = new HttpClientExtension();

	@BeforeEach
	public void before() {
		ourLastBinary = null;
		ourLastBinaryBytes = null;
		ourLastBinaryString = null;
		ourLastId = null;
		ourNextBinary = null;
	}

	@Test
	public void testGetWithNoAccept() throws Exception {

		ourNextBinary = new Binary();
		ourNextBinary.setId("Binary/A/_history/222");
		ourNextBinary.setContent(new byte[]{0, 1, 2, 3, 4});
		ourNextBinary.setSecurityContext(new Reference("Patient/1"));
		ourNextBinary.setContentType("application/foo");

		HttpGet get = new HttpGet(ourServer.getBaseUrl() + "/Binary/A");
		get.addHeader("Content-Type", "application/foo");
		CloseableHttpResponse status = ourClient.execute(get);
		try {
			assertEquals(200, status.getStatusLine().getStatusCode());
			assertEquals("application/foo", status.getEntity().getContentType().getValue());
			assertEquals("Patient/1", status.getFirstHeader(Constants.HEADER_X_SECURITY_CONTEXT).getValue());
			assertEquals("W/\"222\"", status.getFirstHeader(Constants.HEADER_ETAG).getValue());
			assertEquals(ourServer.getBaseUrl() + "/Binary/A/_history/222", status.getFirstHeader(Constants.HEADER_CONTENT_LOCATION).getValue());
			assertNull(status.getFirstHeader(Constants.HEADER_LOCATION));

			byte[] content = IOUtils.toByteArray(status.getEntity().getContent());
			assertThat(content).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}


	@Test
	public void testGetWithAccept() throws Exception {

		ourNextBinary = new Binary();
		ourNextBinary.setId("Binary/A/_history/222");
		ourNextBinary.setContent(new byte[]{0, 1, 2, 3, 4});
		ourNextBinary.setSecurityContext(new Reference("Patient/1"));
		ourNextBinary.setContentType("application/foo");

		HttpGet get = new HttpGet(ourServer.getBaseUrl() + "/Binary/A");
		get.addHeader("Content-Type", "application/foo");
		get.addHeader("Accept", Constants.CT_FHIR_JSON);
		CloseableHttpResponse status = ourClient.execute(get);
		try {
			assertEquals(200, status.getStatusLine().getStatusCode());
			assertEquals("application/json+fhir;charset=utf-8", status.getEntity().getContentType().getValue());
			assertEquals("Patient/1", status.getFirstHeader(Constants.HEADER_X_SECURITY_CONTEXT).getValue());
			assertEquals("W/\"222\"", status.getFirstHeader(Constants.HEADER_ETAG).getValue());
			assertEquals(ourServer.getBaseUrl() + "/Binary/A/_history/222", status.getFirstHeader(Constants.HEADER_CONTENT_LOCATION).getValue());
			assertNull(status.getFirstHeader(Constants.HEADER_LOCATION));

			String content = IOUtils.toString(status.getEntity().getContent(), Charsets.UTF_8);
			assertEquals("{\"resourceType\":\"Binary\",\"id\":\"A\",\"meta\":{\"versionId\":\"222\"},\"contentType\":\"application/foo\",\"securityContext\":{\"reference\":\"Patient/1\"},\"data\":\"AAECAwQ=\"}", content);
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@Test
	public void testPostBinaryWithSecurityContext() throws Exception {
		HttpPost post = new HttpPost(ourServer.getBaseUrl() + "/Binary");
		post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
		post.addHeader("Content-Type", "application/foo");
		post.addHeader(Constants.HEADER_X_SECURITY_CONTEXT, "Encounter/2");
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertNull(ourLastId);
			assertEquals("application/foo", ourLastBinary.getContentType());
			assertEquals("Encounter/2", ourLastBinary.getSecurityContext().getReference());
			assertThat(ourLastBinary.getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});
			assertThat(ourLastBinaryBytes).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@Test
	public void testPostRawBytesBinaryContentType() throws Exception {
		HttpPost post = new HttpPost(ourServer.getBaseUrl() + "/Binary");
		post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
		post.addHeader("Content-Type", "application/foo");
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertNull(ourLastId);
			assertEquals("application/foo", ourLastBinary.getContentType());
			assertThat(ourLastBinary.getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});
			assertThat(ourLastBinaryBytes).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	/**
	 * Technically the client shouldn't be doing it this way, but we'll be accepting
	 */
	@Test
	public void testPostRawBytesFhirContentType() throws Exception {

		Binary b = new Binary();
		b.setContentType("application/foo");
		b.setContent(new byte[]{0, 1, 2, 3, 4});
		String encoded = ourCtx.newJsonParser().encodeResourceToString(b);

		HttpPost post = new HttpPost(ourServer.getBaseUrl() + "/Binary");
		post.setEntity(new StringEntity(encoded));
		post.addHeader("Content-Type", Constants.CT_FHIR_JSON);
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertEquals("application/foo", ourLastBinary.getContentType());
			assertThat(ourLastBinary.getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@Test
	public void testPostRawBytesFhirContentTypeContainingFhir() throws Exception {

		Patient p = new Patient();
		p.getText().setDivAsString("A PATIENT");

		Binary b = new Binary();
		b.setContentType("application/xml+fhir");
		b.setContent(ourCtx.newXmlParser().encodeResourceToString(p).getBytes("UTF-8"));
		String encoded = ourCtx.newJsonParser().encodeResourceToString(b);

		HttpPost post = new HttpPost(ourServer.getBaseUrl() + "/Binary");
		post.setEntity(new StringEntity(encoded));
		post.addHeader("Content-Type", Constants.CT_FHIR_JSON);
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertEquals("application/xml+fhir", ourLastBinary.getContentType());
			assertThat(ourLastBinary.getContent()).containsExactly(b.getContent());
			assertEquals(encoded, ourLastBinaryString);
			assertThat(ourLastBinaryBytes).containsExactly(encoded.getBytes("UTF-8"));
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@Test
	public void testPostRawBytesNoContentType() throws Exception {
		HttpPost post = new HttpPost(ourServer.getBaseUrl() + "/Binary");
		post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertNull(ourLastBinary.getContentType());
			assertThat(ourLastBinary.getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@Test
	public void testPutBinaryWithSecurityContext() throws Exception {
		HttpPut post = new HttpPut(ourServer.getBaseUrl() + "/Binary/A");
		post.setEntity(new ByteArrayEntity(new byte[]{0, 1, 2, 3, 4}));
		post.addHeader("Content-Type", "application/foo");
		post.addHeader(Constants.HEADER_X_SECURITY_CONTEXT, "Encounter/2");
		CloseableHttpResponse status = ourClient.execute(post);
		try {
			assertEquals("Binary/A", ourLastId.getValue());
			assertEquals("Binary/A", ourLastBinary.getId());
			assertEquals("application/foo", ourLastBinary.getContentType());
			assertEquals("Encounter/2", ourLastBinary.getSecurityContext().getReference());
			assertThat(ourLastBinary.getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});
			assertThat(ourLastBinaryBytes).containsExactly(new byte[]{0, 1, 2, 3, 4});
		} finally {
			IOUtils.closeQuietly(status);
		}
	}

	@AfterAll
	public static void afterClassClearContext() throws Exception {
		TestUtil.randomizeLocaleAndTimezone();
	}

	public static class BinaryProvider implements IResourceProvider {
		@Create()
		public MethodOutcome createBinary(@ResourceParam Binary theBinary, @ResourceParam String theBinaryString, @ResourceParam byte[] theBinaryBytes) {
			ourLastBinary = theBinary;
			ourLastBinaryString = theBinaryString;
			ourLastBinaryBytes = theBinaryBytes;
			return new MethodOutcome(new IdType("Binary/001/_history/002"));
		}

		@Override
		public Class<? extends IBaseResource> getResourceType() {
			return Binary.class;
		}

		@Read
		public Binary read(@IdParam IdType theId) {
			return ourNextBinary;
		}

		@Update()
		public MethodOutcome updateBinary(@IdParam IdType theId, @ResourceParam Binary theBinary, @ResourceParam String theBinaryString, @ResourceParam byte[] theBinaryBytes) {
			ourLastId = theId;
			ourLastBinary = theBinary;
			ourLastBinaryString = theBinaryString;
			ourLastBinaryBytes = theBinaryBytes;
			return new MethodOutcome(new IdType("Binary/001/_history/002"));
		}

	}

}
